<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/16
 * Time: 9:54
 */

namespace app\index\controller;


class EnergyStore
{
    //商户管理
    const energy_store = [
        'storeId'=>'',            //门店ID storemanage
        'storeName'=>'',          //店铺名称
        'ammeterNum'=>'',         //电表号
        'ammeterMulti'=>'0',      //电表倍率
        'watermeterNum'=>'',      //水表号
        'watermeterMulti'=>'0',   //水表倍率
        'gasmeterNum'=>'',        //燃气表号
        'gasmeterMulti'=>'0',     //燃气表倍率
        'floor'=>'',              //楼层
    ];
    public static function getData($where, $one = true, $table = 'energy_store')
    {
        return ($one == true) ? getFirst(mongo($table)->where($where)->select()) : mongo($table)->where($where)->select();
    }
    public static function setData($where, $data, $table = 'energy_store')
    {
        mongo($table)->where($where)->update($data);
    }
    public static function insertData($data, $table = 'energy_store')
    {
        mongo($table)->insert($data);
        return mongo($table)->getLastInsID();
    }
    public static function deleteData($where, $table = 'energy_store')
    {
        mongo($table)->where($where)->delete();
    }

    /**
     *  添加商户
        @param string $storeName          店铺名称
        @param string $ammeterNum         电表号
        @param string $ammeterMulti       电表倍率
        @param string $watermeterNum      水表号
        @param string $watermeterMulti    水表倍率
        @param string $gasmeterNum        燃气表号
        @param string $gasmeterMulti      燃气表倍率
        @param string $floor              楼层
     */
    public static function addMerchant($storeName = '', $ammeterNum= '', $ammeterMulti= '', $watermeterNum = '', $watermeterMulti = '', $gasmeterNum = '', $gasmeterMulti= '', $floor = '')
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $storeName         = input('storeName');
        $ammeterNum        = input('ammeterNum');
        $ammeterMulti      = input("ammeterMulti");
        $watermeterNum     = input('watermeterNum');
        $watermeterMulti   = input('watermeterMulti');
        $gasmeterNum       = input('gasmeterNum');
        $gasmeterMulti     = input('gasmeterMulti');
        $floor             = input('floor');

        $insertData = [
            'storeName'      =>$storeName,
            'ammeterNum'     =>$ammeterNum,
            'ammeterMulti'   =>$ammeterMulti,
            'watermeterNum'  =>$watermeterNum,
            'watermeterMulti'=>$watermeterMulti,
            'gasmeterNum'    =>$gasmeterNum,
            'gasmeterMulti'  =>$gasmeterMulti,
            'floor'          =>$floor,
            'storeId'        =>User::getStoreId(),
        ];

        $data = array_merge(self::energy_store, $insertData);
        self::insertData($data); //插入数据
        return RD(SUCCESS);
    }
    /**
     * 获取商户
     * @param string $keyword 搜索关键字
     * @return array
     */
    public static function getMerchants($keyword=null)
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $keyword     = input("keyword");

        $storeId = User::getStoreId();
        $data    = self::getData(['storeId'=>$storeId, 'floor|storeName'=>['like', '.*'.$keyword.'.*']], false);

        foreach ($data as $key=>$value) {
            renameArray($data[$key], 'id', 'storeId');
        }
        //排序
        $my_sort = function ($a, $b) {
            return ($a['floor'] < $b['floor']) ? 1 : (($a['floor'] < $b['floor']) ? 0 : -1);
        };
        usort($data, $my_sort);  //排序

        return RD(SUCCESS, ["total"=>count($data), "stores"=>$data]);
    }
    /**
     * 删除商户
     * @param string $storeId  商户id
     * @return array
     */
    public static function deleteMerchants($storeId = null)
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $storeId = input('storeId');
        if(empty($storeId))
            return RD(PARA_NOT_NULL);

        self::deleteData(['id'=>$storeId]);
        return RD(SUCCESS);
    }
}